<?php
namespace Admin\Controller;
use Think\Controller;
class PersonalController extends AdminController {
    public function pwd(){
        if(IS_POST){
            $pOldpassword = I('oldpassword');
            $pPassword = I('password');
            $pRpassword = I('rpassword');

            if(empty($pPassword) || empty($pOldpassword) || empty($pRpassword)){
                $this->error('密码不能为空');
            }
            if($pPassword != $pRpassword){
                $this->error('新密码两次输入不同');
            }

            $tM = M('admin');
            $tRow = $tM->where('id='.$_SESSION['ru_adm_userid'] . ' and username = \'' .$_SESSION['ru_adm_username']. '\'')->find();

            if (!$tRow|| ($tRow['password'] != get_password($pOldpassword, $tRow['encrypt']))) {
                $this->error('旧密码错误');
            }


            $tData = array(
                'id' => $tRow['id'] ,//保存时会自动为此ID的更新
                'logintime' => time(),
                'loginip' => get_client_ip(),
                'password' => get_password($pPassword, $tRow['encrypt']),
            );
            //更新数据库
            if(!$tM->save($tData)){
                $this->error('异常错误 : 002');
            }
            $this->success('修改成功','pwd');
            exit;
        }
        $this->display();
    }
}
